//
//  LGSportsHistoryDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/9.
//  历史方案--详情

#import "LGSportsHistoryDetailController.h"
#import "LGSportsHistoryDetailHeadView.h"
#import "LGRecoverySectionView.h"
#import "LGRecoveryTrainingCell.h"
@interface LGSportsHistoryDetailController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)LGSportsHistoryDetailHeadView *headView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UIButton *sendBtn;
/**   */
@property (nonatomic , strong)NSDictionary *dataDic;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGSportsHistoryDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"运动方案";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/sport.patient_plan/detail" parameters:@{@"patient_plan_id":self.planid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataDic = returnData;
            self.headView.dataDic = returnData;
            self.dataArray = returnData[@"actions"];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGRecoveryTrainingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.showDesc = NO;
    cell.showTip = NO;
    NSArray *tempArray = self.dataArray[indexPath.section][@"items"];
    if(tempArray.count == 1){
        cell.type = @"separate";
        
    }else if(indexPath.row == 0){
        cell.type = @"top";
        
    }else if (indexPath.row == tempArray.count-1){
        cell.type = @"bottom";
        
    }else{
        cell.type = @"middle";
    }
    
    cell.dataDic = tempArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    return (tempArray.count>0)?viewPix(45):0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    NSDictionary *tempDic = self.dataArray[section];
    if(tempArray.count>0){
        LGRecoverySectionView *headView = [[LGRecoverySectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(45))];
        headView.title = LGNSString(tempDic[@"category_name"]);
        return headView;
    }
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    return (tempArray.count>0)?viewPix(5):0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    CGFloat height = (tempArray.count>0)?viewPix(8):0.01;
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGRecoveryTrainingCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headView;
    }
    return _tableView;
}

-(LGSportsHistoryDetailHeadView *)headView{
    if(!_headView){
        _headView = [[LGSportsHistoryDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(277))];
    }
    return _headView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
